package backend;

import java.util.ArrayList;

public class CommandInput {
	
	public static CommandInput staticInput;
	TaskManager taskManager = new TaskManager();
	private static boolean oneInstance = false;
	
	//private constructor
	private CommandInput(){
	}
	
	//Singleton implementation
	public static CommandInput initInput() {
		if(oneInstance == false) {
			staticInput = new CommandInput();
			oneInstance = true;
		}
		return staticInput;
	}
	
	/**
	 * This method deciphers the instructions entered by the user.
	 * The delimiter (in this case, SPACE) is used to break the string into individual
	 * word and stored in the String array, temp.
	 * 
	 * The first element in temp determines the purpose of the command and calls for 
	 * the respective method to perform the user's desired action.
	 * 
	 * Example: + jogging with david @1700
	 * '+' is detected as the first element, it indicates the adding of a task, therefore
	 * the AddTask() is called.
	 * 
	 * @param command  The command as entered by the user into the Command Input.
	 */
	public void parseCmd(String command) {
		String[] temp;
		
		//delimiter
		String delimiter = " ";
		//given string will be split by the argument delimiter provided.
		temp = command.split(delimiter);
				
		for(int i=0; i<temp.length; i++) {
			if(temp[i].isEmpty())
				temp[i] = "\n";
		}
		
		if(temp[0].equals("+") || temp[0].equals("+*"))
			taskManager.AddTask(temp);
		else if(temp[0].equals("-"))
			taskManager.RemoveTask(temp);
		else if(temp[0].equals("mod") || temp[0].equals("m") )
			taskManager.ModifyTask(temp);
		else if(temp[0].equals("view") || temp[0].equals("v"))
			taskManager.ViewTask(temp);
		else if(temp[0].equals("search") || temp[0].equals("?"))
			taskManager.SearchTask(temp);
		else if(temp[0].equals("done"))
			taskManager.DoneTask(temp);
		else if(temp[0].equals("undone"))
			taskManager.UnDoneTask(temp);
		else if(temp[0].equals("save"))
			taskManager.ExportTask(temp);
		else if(temp[0].equals("import"))
			taskManager.ImportTask(temp);
		else if(temp[0].equals("export"))
			taskManager.ExportTask(temp);
		else
			taskManager.DisplayText("Command error!");
				
		taskManager.DisplayText("Command executed."); //this is for testing
	}
	
	public ArrayList<String> out(){
    	return taskManager.output;
    }
    
    public ArrayList<Task> outTable(){
    	return taskManager.outputViewList;
    }
	
	public Task getNextTask(){
		return taskManager.getNextTask();
	}
	
	public void setTaskOverdue(Task aTask){
		taskManager.setTaskOverdue(aTask);
	}
	
	public int getOverdueCount(){
		int overdueCount = taskManager.getOverdueCount();
		return overdueCount;
	}
	
	public int getUncompletedCount(){
		int everythingcount = taskManager.getUncompletedCount();
		return everythingcount;
	}
	
	public int getDoneCount(){
		int doneCount = taskManager.getDoneCount();
		return doneCount;
	}
	
	public void doneTaskviaPopup(Task aTask) {
		taskManager.DoneTask(aTask);	
	}
	
	/**
	 * This method allows the task to be modified via the Command Input
	 * in the Alarm/Reminder window. It breaks the Command entered followed
	 * by calling the ModifyTask() to make the necessary changes.
	 * 
	 * @param command  The command entered into the Alarm/Remind window
	 * @param aTask  The particular task that generated the Alarm/Reminder
	 */
	public void modifyTaskviaPopup(String command, Task aTask) {
		String[] temp;
		String delimiter = " ";
		temp = command.split(delimiter);
		taskManager.ModifyTask(temp, aTask);	
	}
	
	public int getTaskIndex() {
		return taskManager.getTaskIndex();
	}
	
}